<?php
namespace Back\Controller;
use Think\Controller;

class AdminController extends Controller{
    public function login(){
        if(IS_POST){
            $m = D("manager");
            $mg_name = I("post.mg_name");
            $mg_pwd = I("post.mg_pwd");
            $code = I("post.code");
            $info = $m -> where("mg_name = '{$mg_name}'")->find();
            $verify = new \Think\Verify();
            if(!$verify -> check($code)){
                $this -> error("验证码错误");
            }
            if($info){
                if($info["mg_pwd"] == $mg_pwd){
                    session("mg_id",$info["mg_id"]);
                    session("mg_name",$info["mg_name"]);
                    $this -> success("登录成功",U("Index/index"));
                }else{
                    $this -> error("账号或密码错误");
                }
            }else{
                $this -> error("账号或密码错误");
            }
        }else{
            $this -> display();
        }

    }

    public function logout(){
        session(null);
        $this -> redirect("Admin/login");
    }

    public function verifyimg(){
        $config = array(
            'useImgBg'  =>  false,           // 使用背景图片
            'fontSize'  =>  13,              // 验证码字体大小(px)
            'useCurve'  =>  false,            // 是否画混淆曲线
            'useNoise'  =>  false,            // 是否添加杂点
            'imageH'    =>  30,               // 验证码图片高度
            'imageW'    =>  100,               // 验证码图片宽度
            'length'    =>  4,               // 验证码位数
            'fontttf'   =>  '4.ttf',              // 验证码字体，不设置随机获取
        );
        $verify = new \Think\Verify($config);
        $verify->entry();
    }

    public function checkcode(){
        $code = I("get.code");
        $config = array(
            'reset'     =>  false,
        );
        $verify = new \Think\Verify($config);
        if($verify -> check($code)){
            echo json_encode(array("status"=>1));
        }else{
            echo json_encode(array("status"=>0));
        }
    }

    public function showlist(){
        $info = D("manager") -> join("__ROLE__ on __ROLE__.role_id = __MANAGER__.mg_role_id") -> select();
        $this -> assign("info",$info);
        $this -> display();
    }
}